from WSResult import WSResult
from Registry import Registry
from Common import Common
import re

class SpiderResult(WSResult):
	def as_string(self):
		stat = self.common_stat()

		result = 'Codes stat:\n'
		for code in stat['codes']:
			result += str(code) + ':\n'
			for link in stat['codes'][code]:
				result += '\t' + link + '\n'
			result += '\n'

		result += 'Extensions stat:\n'
		for ext in stat['extensions']:
			result += ext + ':\n'
			for link in stat['extensions'][ext]:
				result += '\t' + link + '\n'
			result += '\n'

		result += 'Slowest links:\n'
		for link in stat['slow_links']:
			result += '\tURL:' + link['url'] + ' time:' + str(link['time']) + ' code:' + str(link['code']) + ':\n'

		result += "\nTotal links count: " + str(Registry.get('mongo').spider_urls.count()) + "\n"

		return result

	def common_stat(self):
		return {'codes': self._get_codes_stat(),
				'slow_links': self._get_slowest_links(),
				'extensions': self._get_extensions()}

	def _get_extensions(self):
		result = {}
		coll = Registry.get('mongo').spider_urls
		links = coll.group({'path': True}, '', {}, 'function () {}')
		links = Common.mongo_result_to_list(links)

		exts = []
		for link in links:
			if link['path'].rfind('.') > -1 and len(link['path']) - link['path'].rfind('.') <= 5:
				exts.append(link['path'][link['path'].rfind('.'):])

		for ext in list(set(exts)):
			if ext not in result:
				result[ext] = []

			links = coll.find({'path': re.compile('\\' + ext + '$')})
			links = Common.mongo_result_to_list(links)

			for link in links:
				result[ext].append(link['path'] + '?' + link['query'] if link['query'] else link['path'])

		return result

	def _get_slowest_links(self):
		result = []
		data = Registry.get('mongo').spider_urls.find({}, {'path': 1, 'query': 1, 'time': 1, 'code': 1}) \
												.sort('time',-1) \
												.limit(int(Registry.get('config')['spider']['stat_slowest_links_count']))

		for link in Common.mongo_result_to_list(data):
			link['url'] = link['path'] + '?' + link['query'] if link['query'] else link['path']
			del link['path'], link['query'], link['_id']
			result.append(link)

		return result

	def _get_codes_stat(self):
		coll = Registry.get('mongo').spider_urls
		result = {}

		codes = coll.group({'code': True}, '', {}, 'function () {}')
		for code in codes:
			links = []
			code = code['code']
			data = coll.find({'code': code}, {'path': 1, 'query': 1})
			for link in Common.mongo_result_to_list(data):
				links.append(link['path'] + '?' + link['query'] if link['query'] else link['path'])
			result[int(code)] = links

		return result
